/**
 * 作者:hanbo
 * 日期:2018/11/1917:44
 * 描述:线绘制
 */
import {VectorBrush} from "./VectorBrush";
import {Line} from "../geometry/Line";

export class WideLinesBrush extends VectorBrush {
    constructor(webGL, mapRender) {
        super(webGL);
        this.mapRender = mapRender;
    }


    /**
     * 批量绘制线
     * @param lines {Line[]} 缓冲区数组
     * @param index {Number} 图层索引
     * @param style {LineStyle|Undefined}
     */
    drawLines(lines, index, style) {
        this._lines = lines;
        this._n = lines.length * 4;
        this._verticesColors = new Float32Array(this._n * 7);
        if (style) {
            this._width = style.width;
            //先将不变的深度值和颜色存储
            for (let i = 0; i < this._lines.length; i++) {
                for (let j = 0; j < 4; j++) {
                    this._verticesColors[i * 28 + j * 7 + 2] = index;
                    this._verticesColors[i * 28 + j * 7 + 3] = style.color.r;
                    this._verticesColors[i * 28 + j * 7 + 4] = style.color.g;
                    this._verticesColors[i * 28 + j * 7 + 5] = style.color.b;
                    this._verticesColors[i * 28 + j * 7 + 6] = style.opacity;
                }
            }
        } else {
            this._width = [];
            let lineStyle;
            //先将不变的深度值和颜色存储
            for (let i = 0; i < this._lines.length; i++) {
                lineStyle = this._lines[i].getStyle();
                this._width.push(lineStyle.width);
                for (let j = 0; j < 4; j++) {
                    this._verticesColors[i * 28 + j * 7 + 2] = index;
                    this._verticesColors[i * 28 + j * 7 + 3] = lineStyle.color.r;
                    this._verticesColors[i * 28 + j * 7 + 4] = lineStyle.color.g;
                    this._verticesColors[i * 28 + j * 7 + 5] = lineStyle.color.b;
                    this._verticesColors[i * 28 + j * 7 + 6] = lineStyle.opacity;
                }
            }
        }

    }

    draw(projection) {
        super.draw(projection);
        this.getPixelWidth();
        this.flushData(this._verticesColors);
        this._compile(this._n);
    }

    /**
     * 重新获取每个像素对应的宽度
     */
    getPixelWidth() {
        if (this.pixelWidth) {
            let unit = this.mapRender.getUnitPerPixel();
            if (this.pixelWidth != unit) {//比例尺变化
                this.pixelWidth = unit;
                this.createWideLine()
            }
        } else {//第一次绘制
            this.pixelWidth = this.mapRender.getUnitPerPixel();
            this.createWideLine()
        }
    }

    /**
     * 生成数据
     */
    createWideLine() {
        let rectanglePath;
        if (!Array.isArray(this._width)) {//宽度相同计算
            for (let i = 0; i < this._lines.length; i++) {
                rectanglePath = Line.lineToRectangle(this._lines[i]._glPath, this._width * this.pixelWidth);
                for (let j = 0; j < 4; j++) {
                    this._verticesColors[i * 28 + j * 7] = rectanglePath[j][0];
                    this._verticesColors[i * 28 + j * 7 + 1] = rectanglePath[j][1];
                }
            }
        } else {
            for (let i = 0; i < this._lines.length; i++) {//宽度不同计算
                rectanglePath = Line.lineToRectangle(this._lines[i]._glPath, this._width[i] * this.pixelWidth);
                for (let j = 0; j < 4; j++) {
                    this._verticesColors[i * 28 + j * 7] = rectanglePath[j][0];
                    this._verticesColors[i * 28 + j * 7 + 1] = rectanglePath[j][1];
                }
            }
        }

    }

    /**
     * 批量绘制线
     * @param n {Number} 线数量
     */
    _compile(n) {
        let currentIndex = 0;
        for (let i = 0; i < this._lines.length; i++) {
            this._webGL.drawArrays(this._webGL.TRIANGLE_FAN, currentIndex, 4);
            currentIndex += 4;
        }
    }

    clear() {
        super.clear();
        this._webGL.disableVertexAttribArray(this.a_Color);
        this._webGL.disableVertexAttribArray(this.a_Position);
    }
}